<template>
    <component
      is="a-modal"
      v-model:visible="visible"
      :width="600"
      :title="title"
      :mask-closable="false"
      :ok-loading="loading"
      @cancel="close"
      @before-ok="submit">
      <!-- 表单信息 start -->
      <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
        <a-row :gutter="10">
          <a-col :span="12">
            <a-form-item label="用户昵称" field="nickname">
              {{ formData.nickname }}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="真实姓名" field="username">
              {{ formData.username||'未填写' }}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用户电话" field="phone">
              {{ formData.phone||'未填写' }}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="当前余额" field="balance1">
              <span class="text-red-500">￥{{ formData.balance1 }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="当前冻结" field="balance2">
              <span class="text-red-500">￥{{ formData.balance2 }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="当前红包" field="balance3">
              <span class="text-red-500">￥{{ formData.balance3 }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="充值类型" field="recharge_type">
              <a-select v-model="formData.type" placeholder="请选择充值类型" :options="rechargeType">
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="充值金额" field="amount">
              <a-input-number v-model="formData.amount" :min="0.01" :max="10000" :precision="2" :step="1" placeholder="请输入充值金额" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="充值备注" field="msg">
              <a-textarea v-model="formData.msg" placeholder="请输入充值备注" allow-clear />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表单信息 end -->
    </component>
  </template>
  
  <script setup>
  import { ref, reactive, computed } from 'vue'
  import { Message, Modal } from '@arco-design/web-vue'
  import api from '@/api/user'
  
  const emit = defineEmits(['success'])
  // 引用定义
  const visible = ref(false)
  const loading = ref(false)
  const formRef = ref()
  const mode = ref('')
  const rechargeType = ref([])
  let title = computed(() => {
    return '用户-充值'
  })
  
  // 表单信息
  const formData = reactive({
    id: null,
    avatar: '',
    nickname: '',
    username: '',
    phone: '',
    balance1: '0.00',
    balance2: '0.00',
    balance3: '0.00',
    type: '',
    amount: null,
    msg: '',
  })
  
  // 验证规则
  const rules = {
    recharge_money: [{ required: true, message: '充值金额必需填写' }],
    recharge_extra: [{ required: true, message: '充值备注必需填写' }],
  }
  
  // 打开弹框
  const open = async (type = 'add') => {
    mode.value = type
    visible.value = true
    formRef.value.resetFields()
    await initPage()
  }
  
  // 初始化页面数据
  const initPage = async () => {
    // 充值类型
    let res = await api.RechargeTypeList()
    if (res.code === 200) {
      rechargeType.value = res.data.list.map(item => ({ value: item.type, label: item.name }))
      console.log('充值类型', rechargeType.value)
    }
  }
  
  // 设置数据
  const setFormData = async (data) => {
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        formData[key] = data[key]
      }
    }
  }
  
  // 数据保存
  const submit = async (done) => {
    const validate = await formRef.value?.validate()
    if (!validate) {
      loading.value = true
      let data = { amount: formData.amount, msg: formData.msg ,type:formData.type,uuid:formData.id}
      let result = await api.Recharge(data)
      if (result.code === 200) {
        Message.success('操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    }
    done(false)
  }
  
  // 关闭弹窗
  const close = () => (visible.value = false)
  
  defineExpose({ open, setFormData })
  </script>
  